//package com.inspur.edp.cef.generator.coreGenerator.base.serializer;
//
//import com.fasterxml.jackson.core.JsonParser;
//import com.fasterxml.jackson.databind.DeserializationContext;
//import com.fasterxml.jackson.databind.JsonNode;
//import com.inspur.edp.bef.bizentity.GspBusinessEntity;
//import com.inspur.edp.caf.generator.baseInfo.ParameterInfo;
//import com.inspur.edp.caf.generator.baseInfo.TypeInfo;
//import com.inspur.edp.caf.generator.method.ClassMethodGenerator;
//import com.inspur.edp.caf.generator.utils.ExpressionUtils;
//import com.inspur.edp.cef.designtime.api.IGspCommonField;
//import com.inspur.edp.cef.designtime.api.element.GspAssociation;
//import com.inspur.edp.cef.designtime.api.element.GspElementObjectType;
//import com.inspur.edp.cef.designtime.api.entity.GspCommonField;
//import com.inspur.edp.cef.entity.changeset.IChangeDetail;
//import com.inspur.edp.cef.entity.changeset.ValueObjModifyChangeDetail;
//import com.inspur.edp.cef.generator.coreGenerator.base.entityGenerator.CefDataTypeImplGeneratorContext;
//import com.inspur.edp.cef.generator.coreGenerator.entityGenerator.serializer.CMChangeJsonConvertorGenUtils;
//import com.inspur.edp.cef.generator.overall.GenUtils;
//import com.inspur.edp.das.commonmodel.entity.GspCommonElement;
//import com.inspur.edp.udt.designtime.api.entity.UnifiedDataTypeDef;
//import java.io.IOException;
//import java.util.ArrayList;
//import java.util.Objects;
//import javax.ws.rs.NotSupportedException;
//import org.eclipse.jdt.core.dom.Assignment;
//import org.eclipse.jdt.core.dom.Block;
//import org.eclipse.jdt.core.dom.CatchClause;
//import org.eclipse.jdt.core.dom.ClassInstanceCreation;
//import org.eclipse.jdt.core.dom.Expression;
//import org.eclipse.jdt.core.dom.IfStatement;
//import org.eclipse.jdt.core.dom.InfixExpression;
//import org.eclipse.jdt.core.dom.InfixExpression.Operator;
//import org.eclipse.jdt.core.dom.MethodInvocation;
//import org.eclipse.jdt.core.dom.Modifier.ModifierKeyword;
//import org.eclipse.jdt.core.dom.SingleVariableDeclaration;
//import org.eclipse.jdt.core.dom.Statement;
//import org.eclipse.jdt.core.dom.ThrowStatement;
//import org.eclipse.jdt.core.dom.TryStatement;
//
//public class ReadEnrichedModifyPropValueGenerator extends ClassMethodGenerator {
//  protected static final String paramReader = "reader";
//  protected static final String paramSerializer = "serializer";
//  protected UnifiedDataTypeDef udtDef;
//  private final GspCommonField field;
//  protected CefDataTypeImplGeneratorContext dataTypeGenContext;
//
//  public ReadEnrichedModifyPropValueGenerator(
//      CefDataTypeImplGeneratorContext dataTypeGenContext, GspCommonField field) {
//
//    this.dataTypeGenContext = dataTypeGenContext;
//    this.field = field;
//  }
//
//  public static String getName(IGspCommonField field) {
//    Objects.requireNonNull(field);
//    return "readModifyPropertyValue_" + field.getLabelID();
//  }
//
//  static String nodeVarName = "node";
//  static String valueObjChangeVarName = "valueObjChange";
//
//  @Override
//  protected void beforeGenerate() {
//    super.beforeGenerate();
//    udtDef =
//        dataTypeGenContext
//            .coreGenContext
//            .getCefGenContext()
//            .getMetadataContent(field.getUdtPkgName(), field.getUdtID());
//  }
//
//  @Override
//  protected Block buildMethodBody() {
//    Block block = ast.newBlock();
//    addImport(JsonParser.class.getName());
//    addImport(JsonNode.class.getName());
//    addImport(IOException.class.getName());
////    addImport(ObjectMapper.class.getName());
//    addImport(ValueObjModifyChangeDetail.class.getName());
//
//    block
//        .statements()
//        .add(
//            GenUtils.buildVarDeclarationStatement(
//                ast, JsonNode.class, nodeVarName, ast.newNullLiteral()));
//    block.statements().add(readValueAsTreeStatement());
//    block.statements().add(readNestedChangeStatement());
//    // String key = modifyChange.getPropertyChanges().keySet().stream().findFirst().get();
//    block.statements().add(getKeyStatement());
//    // Object value = modifyChange.getPropertyChanges().get(key);
//    block.statements().add(getValueStatement());
//    // UserUdtInfo info = new UserUdtInfo((SalesOrder0912UserUdtInfo) value);
//    block.statements().add(getCreateInfoStatement());
//
//    for (IGspCommonField gspCommonField :
//        field.getChildAssociations().get(0).getRefElementCollection()) {
//      // TODO 后续支持带出字段是关联
//      if (gspCommonField.getIsFromAssoUdt()
//          || gspCommonField.getObjectType() == GspElementObjectType.Association
//          || gspCommonField.getObjectType() == GspElementObjectType.DynamicProp) {
//        continue;
//      }
//      block.statements().add(getSetEnrichedValue(gspCommonField));
//    }
//    // valueObjChangeVarName.getPropertyChanges().put(key, userUdtInfo);
//    block.statements().add(getPutInfoChange());
//    block.statements().add(GenUtils.buildReturnStatement(ast, valueObjChangeVarName));
//    return block;
//  }
//
//  private Statement getPutInfoChange() {
//    MethodInvocation getPropChanges = ast.newMethodInvocation();
//    getPropChanges.setExpression(ast.newSimpleName(valueObjChangeVarName));
//    getPropChanges.setName(ast.newSimpleName("getPropertyChanges"));
//
//    MethodInvocation putInvocation = ast.newMethodInvocation();
//    putInvocation.setExpression(getPropChanges);
//    putInvocation.setName(ast.newSimpleName("put"));
//    putInvocation.arguments().add(ast.newSimpleName("key"));
//    putInvocation.arguments().add(ast.newSimpleName("info"));
//
//    return ast.newExpressionStatement(putInvocation);
//  }
//
//  private Block getSetEnrichedValue(IGspCommonField gspCommonField) {
//    // {
//    //      JsonNode enrichedValue = getEnrichedValue(node, "UserUdt_Code");
//    //      if (enrichedValue != null) {
//    //        userUdtInfo.setUserUdt_Code(enrichedValue.toString());
//    //      }
//    // }
//    Block block = ast.newBlock();
//
//    MethodInvocation getEnrichedValue = ast.newMethodInvocation();
//    getEnrichedValue.setName(ast.newSimpleName("getEnrichedValue"));
//    getEnrichedValue.arguments().add(ast.newSimpleName(nodeVarName));
//    getEnrichedValue
//        .arguments()
//        .add(ExpressionUtils.getStringLiteral(ast, gspCommonField.getLabelID()));
//
//    block
//        .statements()
//        .add(
//            GenUtils.buildVarDeclarationStatement(
//                ast, JsonParser.class, "enrichedValue", getEnrichedValue));
//
//    InfixExpression infixExpression = ast.newInfixExpression();
//    infixExpression.setLeftOperand(ast.newSimpleName("enrichedValue"));
//    infixExpression.setOperator(Operator.NOT_EQUALS);
//    infixExpression.setRightOperand(ast.newNullLiteral());
//
//    IfStatement ifStatement = ast.newIfStatement();
//    ifStatement.setExpression(infixExpression);
//    Block ifThenBlock = ast.newBlock();
//    MethodInvocation setEnrichedValue = ast.newMethodInvocation();
//    setEnrichedValue.setName(ast.newSimpleName("set" + gspCommonField.getLabelID()));
//    setEnrichedValue.setExpression(ast.newSimpleName("info"));
//
//    setEnrichedValue.arguments().add(buildReadProperty(gspCommonField, ast.newSimpleName("enrichedValue")));
//    ifThenBlock.statements().add(ast.newExpressionStatement(setEnrichedValue));
//    ifStatement.setThenStatement(ifThenBlock);
//
//    block.statements().add(ifStatement);
//    return block;
//  }
//
//  private Expression buildReadProperty(IGspCommonField field, Expression valueExpr) {
//    if (field.getIsUdt()) {
//      return buildUdtReadProperty(field, valueExpr);
//    } else {
//      switch (field.getObjectType()) {
//        case Enum:
//          return buildEnumReadProperty(field, valueExpr);
//        case None:
//          return buildBasicReadProperty(field, valueExpr);
//        default:
//          throw new NotSupportedException();
//      }
//    }
//  }
//
//  private Expression buildBasicReadProperty(IGspCommonField field, Expression valueExpr) {
//    String readMethodName = null;
//    switch (field.getMDataType()) {
//      case String:
//      case Text:
//        readMethodName = CMChangeJsonConvertorGenUtils.mName_ReadString;
//        break;
//      case Integer:
//        readMethodName = CMChangeJsonConvertorGenUtils.mName_ReadInt;
//        break;
//      case Decimal:
//        readMethodName = CMChangeJsonConvertorGenUtils.mName_ReadDecimal;
//        break;
//      case Date:
//      case DateTime:
//        if (SerItemGenUtil.getEnableStdDateTime(this.dataTypeGenContext)) {
//          readMethodName = CMChangeJsonConvertorGenUtils.mName_ReadStdDateTime;
//        } else {
//          readMethodName = CMChangeJsonConvertorGenUtils.mName_ReadDateTime;
//        }
//        break;
//      case Boolean:
//        readMethodName = CMChangeJsonConvertorGenUtils.mName_ReadBool;
//        break;
//      case Binary:
//        readMethodName = CMChangeJsonConvertorGenUtils.mName_ReadBytes;
//        break;
//      default:
//        throw new RuntimeException("错误的类型" + field.getMDataType());
//    }
//
//    MethodInvocation methodInvocation = ast.newMethodInvocation();
//    methodInvocation.setName(ast.newSimpleName(readMethodName));
//    methodInvocation.arguments().add(valueExpr);
//    return methodInvocation;
//  }
//
//  private Expression buildUdtReadProperty(IGspCommonField field, Expression valueExpr) {
//    throw new RuntimeException("暂不支持be上对关联udt字段增加udt类型的带出字段：" + field.getLabelID());
//  }
//
//  private Expression buildEnumReadProperty(IGspCommonField field, Expression valueExpr) {
//    GspAssociation gspAssociation =
//        udtDef.getContainElements().get(0).getChildAssociations().get(0);
//    GspBusinessEntity entity =
//        dataTypeGenContext
//            .coreGenContext
//            .getCefGenContext()
//            .getMetadataContent(
//                gspAssociation.getRefModelPkgName(), gspAssociation.getRefModelID());
//    String enumTypeName =
//        entity.getEntityAssemblyInfo().getDefaultNamespace()
//            + "."
//            + entity.findElementById(field.getRefElementId()).getEnumTypeName();
//
//    MethodInvocation methodInvocation = ast.newMethodInvocation();
//    methodInvocation.setName(ast.newSimpleName(CMChangeJsonConvertorGenUtils.mName_ReadEnum));
//    methodInvocation.arguments().add(valueExpr);
//    methodInvocation.arguments().add(GenUtils.buildTypeLiteral(ast, enumTypeName));
//    return methodInvocation;
//  }
//
//  private Statement getCreateInfoStatement() {
//    String infoType =
//        dataTypeGenContext.coreGenContext.getCefGenContext().getEntityGenContext().getPackageName()
//            + "."
//            + ((GspCommonElement) field).getAssociationTypeName();
//    String udtInfoType =
//        udtDef.getEntityAssemblyInfo().getDefaultNamespace()
//            + "."
//            + udtDef.getContainElements().get(0).getLabelID()
//            + "Info";
//    ClassInstanceCreation classInstanceCreation = ast.newClassInstanceCreation();
//    classInstanceCreation.setType(ast.newSimpleType(ast.newName(infoType)));
//    classInstanceCreation
//        .arguments()
//        .add(GenUtils.buildCastExpression(ast, udtInfoType, ast.newSimpleName("value")));
//    return GenUtils.buildVarDeclarationStatement(ast, infoType, "info", classInstanceCreation);
//  }
//
//  private Statement getValueStatement() {
//    return GenUtils.buildVarDeclarationStatement(
//        ast,
//        Object.class,
//        "value",
//        GenUtils.getMethodInvocation(
//            ast,
//            GenUtils.getMethodInvocation(
//                ast, ast.newName(valueObjChangeVarName), "getPropertyChanges"),
//            "get",
//            "key"));
//  }
//
//  private Statement getKeyStatement() {
//    MethodInvocation methodInvocation = ast.newMethodInvocation();
//    methodInvocation.setExpression(ast.newName(valueObjChangeVarName));
//    methodInvocation.setName(ast.newSimpleName("getPropertyChanges"));
//    return GenUtils.buildVarDeclarationStatement(
//        ast,
//        String.class,
//        "key",
//        ExpressionUtils.getMethodInvocation(
//            ast,
//            ExpressionUtils.getMethodInvocation(
//                ast,
//                ExpressionUtils.getMethodInvocation(
//                    ast,
//                    ExpressionUtils.getMethodInvocation(ast, methodInvocation, "keySet"),
//                    "stream"),
//                "findFirst"),
//            "get"));
//  }
//
//  private Statement readNestedChangeStatement() {
//    MethodInvocation readNested = ast.newMethodInvocation();
//    readNested.setName(ast.newSimpleName(CMChangeJsonConvertorGenUtils.mName_ReadNestedChange));
//    readNested.arguments().add(ExpressionUtils.getStringLiteral(ast, udtDef.getUdtType()));
//    readNested.arguments().add(ast.newSimpleName(nodeVarName));
//    readNested.arguments().add(ast.newSimpleName(paramSerializer));
//
//    if (SerItemGenUtil.getEnableStdDateTime(dataTypeGenContext)) {
//      SerItemGenUtil.addGetNestedSerializeContextArg(ast, readNested);
//    }
//    return GenUtils.buildVarDeclarationStatement(
//        ast,
//        ValueObjModifyChangeDetail.class,
//        valueObjChangeVarName,
//        GenUtils.buildCastExpression(ast, ValueObjModifyChangeDetail.class, readNested));
//  }
//
//  private Statement readValueAsTreeStatement() {
//    MethodInvocation readAsTree = ast.newMethodInvocation();
//    readAsTree.setName(ast.newSimpleName("readValueAsTree"));
//    readAsTree.setExpression(ast.newSimpleName(paramReader));
//    Assignment assignment = ast.newAssignment();
//    assignment.setRightHandSide(readAsTree);
//    assignment.setLeftHandSide(ast.newSimpleName(nodeVarName));
//    Block body = ast.newBlock();
//    body.statements().add(ast.newExpressionStatement(assignment));
//    TryStatement tryStatement = ast.newTryStatement();
//    tryStatement.setBody(body);
//
//    CatchClause catchClause = ast.newCatchClause();
//    SingleVariableDeclaration singleVariableDeclaration = ast.newSingleVariableDeclaration();
//    singleVariableDeclaration.setType(
//        new com.inspur.edp.caf.generator.baseInfo.TypeInfo(IOException.class).getType(ast));
//    singleVariableDeclaration.setName(ast.newSimpleName("e"));
//    catchClause.setException(singleVariableDeclaration);
//    Block catchBodyBlock = ast.newBlock();
//    ThrowStatement throwStatement = ast.newThrowStatement();
//    ClassInstanceCreation classInstanceCreation = ast.newClassInstanceCreation();
//    classInstanceCreation.setType(
//        new com.inspur.edp.caf.generator.baseInfo.TypeInfo(RuntimeException.class).getType(ast));
//    classInstanceCreation.arguments().add(ast.newSimpleName("e"));
//    throwStatement.setExpression(classInstanceCreation);
//    catchBodyBlock.statements().add(throwStatement);
//    catchClause.setBody(catchBodyBlock);
//    tryStatement.catchClauses().add(catchClause);
//
//    return tryStatement;
//  }
//
//  @Override
//  protected String getMethodName() {
//    return getName(field);
//  }
//
//  @Override
//  protected TypeInfo getReturnType() {
//    return new TypeInfo(IChangeDetail.class);
//  }
//
//  @Override
//  protected ArrayList<ModifierKeyword> getAccessModifiers() {
//    return new java.util.ArrayList<ModifierKeyword>(
//        java.util.Arrays.asList(new ModifierKeyword[] {ModifierKeyword.PRIVATE_KEYWORD}));
//  }
//
//  @Override
//  protected ArrayList<ParameterInfo> getParameterCollection() {
//    ParameterInfo readerInfo = new ParameterInfo();
//    readerInfo.setParamName(paramReader);
//    readerInfo.setParamType(new TypeInfo(JsonParser.class));
//
//    ParameterInfo serializerInfo = new ParameterInfo();
//    serializerInfo.setParamName(paramSerializer);
//    serializerInfo.setParamType(new TypeInfo(DeserializationContext.class));
//
//    return new java.util.ArrayList<ParameterInfo>(
//        java.util.Arrays.asList(new ParameterInfo[] {readerInfo, serializerInfo}));
//  }
//}
